package com.zw.mes.module.system.controller;

import com.zw.mes.base.BaseController;
import com.zw.mes.common.JsonResult;
import com.zw.mes.common.ValidResult;
import com.zw.mes.entity.sys.Dept;
import com.zw.mes.module.system.service.DeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Description: 部门接口
 *
 * @Author: China.sgl
 * @Date: 2020/8/3 16:11
 */
@Controller
@RequestMapping("/sys/dept")
@SuppressWarnings("all")
public class DeptController extends BaseController {

    @Autowired
    private DeptService deptService;

    @GetMapping
    @RequiresPermissions({"system:dept:view"})
    public String page(Model model) {
        return "system/dept/page";
    }

    /**
     * 系统部门数据
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"system:dept:view"})
    public List<Dept> list(Dept dept) {
        return deptService.findDeptByCondition(dept);
    }

    /**
     * 部门名称唯一验证
     */
    @ResponseBody
    @PostMapping("/checkUnique")
    @RequiresPermissions({"system:dept:view"})
    public ValidResult checkUnique(Dept dept) {
        Dept m = deptService.findDeptByName(dept.getName());
        if (dept.getId() == null) {
            return ValidResult.result(m);
        }
        if (m == null || dept.getId().equals(m.getId())) {
            return ValidResult.SUCCESS;
        }
        return ValidResult.FAIL;
    }

    /**
     * 创建部门
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({"system:dept:edit"})
    public JsonResult save(Dept dept) {
        return isNotNull(deptService.saveDept(dept));
    }

    /**
     * 修改部门
     */
    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions({"system:dept:edit"})
    public JsonResult update(Dept dept) {
        return isNotNull(deptService.updateDept(dept));
    }

    /**
     * 删除部门
     */
    @ResponseBody
    @PostMapping("/del")
    @RequiresPermissions({"system:dept:edit"})
    public JsonResult del(Integer id) {
        return jsonResult(deptService.delDept(id));
    }

}
